﻿//Please add references

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using YwangHRMSys.DBUtility;
using YwangHRMSys.Model;


namespace YwangHRMSys.DAL
    {
        /// <summary>
        ///     数据访问类:DepartmentInfoDAL
        /// </summary>
        public class DepartmentInfoDAL
            {
                #region 开始生成tomodel提高代码的重用性

                /// 开始生成tomodel提高代码的重用性
                private DepartmentInfoModel ToModel( DataRow row )
                {
                    var model = new DepartmentInfoModel();
                    model.Id = (Guid) SqlHelper.FromDbValue(row["Id"]);
                    model.DepartmentName = (String) SqlHelper.FromDbValue(row["DepartmentName"]);
                    model.IsDelete = (Boolean) SqlHelper.FromDbValue(row["IsDelete"]);
                    return model;
                } //tomodel的结束。

                #endregion

                #region listAll开始

                //查询所有DepartmentInfo的数据列表
                public IEnumerable <DepartmentInfoModel> ListAll()
                {
                    var list = new List <DepartmentInfoModel>();
                    DataTable table = SqlHelper.ExecuteDataTable("SELECT * FROM dbo.DepartmentInfo WHERE IsDelete=0 ");
                    foreach (DataRow row in table.Rows)
                        {
                            DepartmentInfoModel model = ToModel(row);
                            list.Add(model);
                        }
                    return list;
                } //listAllDepartmentInfo结束

                #endregion

                #region 添加DepartmentInfo信息

                public
                        void InsertDepartmentInfo
                        (
                        DepartmentInfoModel
                                model )
                {
                    SqlHelper.ExecuteNonQuery
                            (
                                    "INSERT INTO DepartmentInfo  (DepartmentName,IsDelete  ) values (@DepartmentName,@IsDelete)",
                                    
                                    new SqlParameter("@DepartmentName", model.DepartmentName),
                                    new SqlParameter("@IsDelete",false));
                }

                #endregion

                #region  根据ID修改DepartmentInfo的信息

                public void UpdateDepartmentInfo( DepartmentInfoModel model )
                {
                    SqlHelper.ExecuteNonQuery
                            (
                                    "  UPDATE  DepartmentInfo SET  DepartmentName = @DepartmentName  WHERE  Id = @Id",
                                    new SqlParameter("@Id", model.Id),
                                    new SqlParameter("@DepartmentName", model.DepartmentName)
                                   );
                }

                #endregion

                #region  根据ID来软删除DepartmentInfo的信息

                public void DeleteDepartmentInfoById(DepartmentInfoModel model)
                {
                    SqlHelper.ExecuteNonQuery
                            (
                                    "UPDATE  DepartmentInfo SET  IsDelete=1  WHERE  Id = @Id",
                                    new SqlParameter("@Id", model.Id)
                                   
                                   );
                }

                #endregion




                #region  根据ID来查询DepartmentInfo的信息

                public DepartmentInfoModel GetByDepartmentInfoID( Guid Id )
                {
                    DataTable table = SqlHelper.ExecuteDataTable
                            (
                                    " SELECT Id,DepartmentName,IsDelete FROM  DepartmentInfo  WHERE  Id = @Id",
                                    new SqlParameter("@Id", Id));
                    if (table.Rows.Count <= 0)
                        {
                            return null;
                        }
                    if (table.Rows.Count == 1)
                        {
                            return ToModel(table.Rows[0]);
                        }
                    throw new Exception();
                }

                #endregion

                //根据ID来查询DepartmentInfo的信息
            }

        //当前DepartmentInfoDAL类结束
    }